<?php
/* 
 * class.msg.php
 * 我的家园-用户信息
 * @author samuel
 * @date 2009-05-24
 */
include_once 'class.myhomeBase.php';
class myhome_msg extends myhomeBase {

    function  __construct($core) {
        parent::__construct();
        //参数处理
        $this->step = $this->params['step'] ? strtolower(trim($core->params['step'])) : 'index';
	    $this->user_id = $this->params['uid'] ? (int)$this->params['uid'] : 1;
	    $this->type = $this->params['type'] ? strtolower(trim($this->params['type'])) : 'msg';
        $this->typecode = $this->type == 'msg' ? 1 : 2;
        $this->page = $this->params['page'] ? (int)$this->params['page'] : 1;
	    if(!$this->user_id){
		    $this->error = "用户ID不合法！";
		    $this->jumpError();
		    exit;
	    }

        $run = "run_" . $this->step;
        
        //header("Content-type: text/vnd.wap.wml");
        $this->$run();
    }

    function run_index(){
    	//WAP页面
	    //header("Content-type: text/vnd.wap.wml");
	    //TITLE
	    $page_title = "我的家园-用户短信";
	    $content = "";
	    $content .= "<img src='' alt='头像' /><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."'>动态</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userinfo&amp;uid=".$this->user_id."'>信息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;uid=".$this->user_id."'>设置</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=msg&amp;uid=".$this->user_id."'>消息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."'>收藏</a>｜<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id."'>好友</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=pubSeller&amp;uid=".$this->user_id."'>发布商家</a>｜<a href='".$_SERVER['PHP_SELF']."?act=pubDiscount&amp;uid=".$this->user_id."'>发布优惠</a><br/>";
	
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."&amp;act=msg&amp;type=msg'>短信</a>｜<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."&amp;act=msg&amp;type=sys'>系统</a><br/>";
	    
	    
	    $pagesize = 10;
	    $displaypage = 5;
	    $page = $this->page;
	    $current_url = $_SERVER['PHP_SELF']."?act=msg&amp;uid=".$this->user_id."&amp;type=".$this->type;

        $counts = $this->getUserMsg('', '', $this->user_id, $this->typecode, '',true);
        $totalPages = ceil($counts/$pagesize);
        $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
	    $arr_limit = array('offset'=>($page-1)*$pagesize, 'pagesize'=>$pagesize);
	    //获取用户消息
	    $msgs = $this->getUserMsg('','',$this->user_id,$this->typecode, '', false, $arr_limit);
	    $content .= $pager."<br/>";
	    if($msgs){
	        foreach($msgs as $msg){
	            $sender = $this->core->load('user::userBase')->getInfo(array('table'=>'fun_user', 'primary'=>'UserID'), $msg['PosterID']);
	            //var_dump($sender);
	            $content .= $sender['UserNickName']."   ".$msg['Sendtime']."<br/>";
	            $content .= "<a href='".$_SERVER['PHP_SELF']."?act=msg&amp;step=showinfo&amp;type=".$this->type."&amp;mid=".$msg['MsgID']."&amp;uid=".$this->user_id."'>".$msg['MsgContent'] . "</a><br/>";
	        }
	    }
	    
	    $content .= $pager."<br/>";
            

	    $content .= "<br/><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }

    function run_showinfo(){
        $mid = $this->params['mid'];
        $page_title = "我的家园-短消息";
        $backurl = $_SERVER['PHP_SELF'] . '?act=msg&amp;uid=' . $this->user_id;
        $posturl = $_SERVER['PHP_SELF'] . '?act=msg&amp;step=reply_do&amp;uid=' . $this->user_id . '&amp;mid=' . $mid;
	    $content = "";
        
        if(!$mid){
            $this->error = "对不起，您查看的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $tag = $this->getUserMsg($mid, '', '', $this->typecode, '');
        
        if(!$tag){
            $this->error = "对不起，您查看的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        
        $content .= "<a href='".$backurl."'>返回上级</a><br/><br/>";

        if($this->type == 'msg'){
            $content .= "<input type='text' name='msg_replay' value='' maxlength='20' size='15' />";
            $content .= "<anchor title=\"回复\">回复<go href=\"".$posturl."\" method=\"post\">
                                <postfield name=\"type\" value=\"msg\" />
                                <postfield name=\"msg_replay\" value=\"$(msg_replay)\" />
                                <postfield name=\"receiver_id\" value=\"".$info['PosterID']."\" />
                                </go></anchor><br/>";
            //分页
            $pagesize = 1;
	        $displaypage = 5;
	        $page = $this->page;
	        $friend_id = $tag['PosterID'];
	        $current_url = $_SERVER['PHP_SELF']."?act=msg&amp;step=showinfo&amp;uid=".$this->user_id."&amp;type=".$this->type."&amp;mid=".$mid;

            $counts = $this->getUserFriendMsg($this->user_id, $friend_id,'',true);
            $totalPages = ceil($counts/$pagesize);
            $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
            $content .= $pager."<br/>";
            //获取短消息内容
            $arr_limit = array('offset'=>($page-1)*$pagesize, 'pagesize'=>$pagesize);
            $infos = $this->getUserFriendMsg($this->user_id, $friend_id,'',false, $arr_limit);
            
            if($infos){
                foreach($infos as $info){
                    if($info['PosterID'] == $this->user_id){
                        $sender['UserNickName'] = "我";
                    }else{
                        $sender = $this->core->load('user::userBase')->getInfo(array('table'=>'fun_user', 'primary'=>'UserID'), $info['PosterID']);
                    }
                    
                    $content .= $sender['UserNickName'] . " " .$info['Sendtime'] ."<br/>";
                    $content .= $info['MsgContent'] . "<br/>";

                }
            }
            $content .= $pager."<br/>";
            $content .= "<input type='text' name='msg_replay' value='' maxlength='20' size='15' />";
            $content .= "<anchor title=\"回复\">回复<go href=\"".$posturl."\" method=\"post\">
                                <postfield name=\"type\" value=\"msg\" />
                                <postfield name=\"msg_replay\" value=\"$(msg_replay)\" />
                                <postfield name=\"receiver_id\" value=\"".$info['PosterID']."\" />
                                </go></anchor><br/>";
        }else{
            $info = $this->getUserMsg($mid, '', '', $type="");
            //$sender = $this->core->load('user::userBase')->getInfo(array('table'=>'fun_user', 'primary'=>'UserID'), $info['PosterID']);
            $content .= $info['Sendtime'] . "<br/>";
            $content .= $info['MsgContent'] . "<br/>";
        }
        

        $content .= "<br/><br/>";
        $content .= "<a href='".$backurl."'>返回上级</a><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
        
    }

    function run_reply_do(){
        $mid = $this->params['mid'];
        $page_title = "我的家园-短消息";
        $backurl = $_SERVER['PHP_SELF'] . '?act=msg&amp;uid=' . $this->user_id;
	    $content = "";

        if(!$mid){
            $this->error = "对不起，您回复的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $tag = $this->getUserMsg($mid, '', '', $this->typecode, '', true);
        if(!$tag){
            $this->error = "对不起，您回复的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $receiver_id = $this->params['receiver_id'];
        $msgcontent = htmlspecialchars($this->params['msg_replay']);
        $sql = "INSERT INTO `fun_msg` SET `PosterID`='".$this->user_id."', `ReceiverID`='".$receiver_id."', `MsgContent`='".$msgcontent."', `Sendtime`=now(), `Msgtype`='1', `Isread`='1'";
        $res = $this->core->db->query($sql);
        if($res){
            $content = "回复成功！";
        }else{
            $content = "回复失败！";
        }

    	$this->jumpPrompt($content, $page_title, $backurl);
    }
    

}


?>
